﻿using AutoMapper;
using FinancialManagement.Application.Command.SaleContractCommand;
using FinancialManagement.Infrstructre.Interface;
using MediatR;
using SqlSugar;

namespace FinancialManagement.Application.CommandHandler.SaleContractCommandHandlers
{
    public class SaleContractDelCommandHandlers : IRequestHandler<SaleContractDelCommand, int>
    {
        IMapper mapper;
        private readonly ISaleContractProductRepository SRep;
        private readonly ISaleContractRepository CRep;
        ISqlSugarClient db;
        public SaleContractDelCommandHandlers(IMapper mapper, ISaleContractProductRepository SRep, ISaleContractRepository CRep, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.SRep = SRep;
            this.CRep = CRep;
            this.db = db;
        }
        public async Task<int> Handle(SaleContractDelCommand request, CancellationToken cancellationToken)
        {
            var list = (await CRep.GetAllAsyns()).Where(c => request.SaleContractId.Contains(c.SaleContractId)).ToList();
            foreach (var item in list)
            {
                item.IsDel = true;
                item.DelTime = DateTime.Now;
                await CRep.UpdateAsync(item);
            }
            var lists = (await SRep.GetAllAsyns()).Where(c => c.SCcontractNumber==list.First().SCcontractNumber).ToList();
            foreach (var item in lists)
            {
                item.IsDel = true;
                item.DelTime = DateTime.Now;
                await SRep.UpdateAsync(item);
            }
            return 1;

        }
    }
}
